﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hutech.CRM.Library.Models.BusinessAccessLayer.BusinessObjects;
using Hutech.CRM.Library.Models.Entities;
using System.Data;
using Hutech.CRM.Library.Utilities;

namespace Hutech.CRM.Library.Models.BusinessAccessLayer
{
    class DanhMucThongTinBAL : IDanhMucThongTinBAL
    {
        #region Variable
        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of DanhMucThongTin.
        /// </summary>
        /// <param name="danhMucGuid"> danhMucGuid </param>
        private void GetDanhMucThongTin(Guid danhMucGuid)
        {
            using (IDataReader reader = DanhMucThongTinDAL.GetOne(
                danhMucGuid))
            {
                PopulateFromReader(reader);
            }

        }

        private DanhMucThongTin PopulateFromReader(IDataReader reader)
        {
            DanhMucThongTin item = new DanhMucThongTin();
            if (reader.Read())
            {
                item.DanhMucGuid = new Guid(reader[ConstantVariable.LIST_DANHMUCTHONGTIN_GUID].ToString());
                item.DanhMucID = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_ID].ToString();
                item.DanhMucName = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_NAME].ToString();
                item.LoaiDanhMucGuid = new Guid(reader[ConstantVariable.LIST_DANHMUCTHONGTIN_KINDGUID].ToString());
                item.LoaiDanhMucName = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_KINDNAME].ToString();
                item.IsDeleted = Convert.ToBoolean(reader[ConstantVariable.COLUMN_ISDELETED]);
                item.IsActive = Convert.ToBoolean(reader[ConstantVariable.COLUMN_ISACTIVE]);

            }
            return item;

        }

        /// <summary>
        /// Persists a new instance of DanhMucThongTin. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create(DanhMucThongTin item)
        {
            item.DanhMucGuid = Guid.NewGuid();

            int rowsAffected = DanhMucThongTinDAL.Create(item
               );

            return (rowsAffected > 0);

        }


        /// <summary>
        /// Updates this instance of DanhMucThongTin. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update(DanhMucThongTin item)
        {

            return DanhMucThongTinDAL.Update(
                item);

        }

        #endregion
        private static List<DanhMucThongTin> LoadListFromReader(IDataReader reader)
        {
            List<DanhMucThongTin> danhMucThongTinList = new List<DanhMucThongTin>();
            try
            {
                while (reader.Read())
                {
                    DanhMucThongTin danhMucThongTin = new DanhMucThongTin();
                    danhMucThongTin.DanhMucGuid = new Guid(reader[ConstantVariable.LIST_DANHMUCTHONGTIN_GUID].ToString());
                    danhMucThongTin.DanhMucID = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_ID].ToString();
                    danhMucThongTin.DanhMucName = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_NAME].ToString();
                    danhMucThongTin.LoaiDanhMucGuid = new Guid(reader[ConstantVariable.LIST_DANHMUCTHONGTIN_KINDGUID].ToString());
                    danhMucThongTin.LoaiDanhMucName = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_KINDNAME].ToString();
                    danhMucThongTin.IsDeleted = Convert.ToBoolean(reader[ConstantVariable.COLUMN_ISDELETED]);
                    danhMucThongTin.IsActive = Convert.ToBoolean(reader[ConstantVariable.COLUMN_ISACTIVE]);
                    danhMucThongTinList.Add(danhMucThongTin);

                }
            }
            finally
            {
                reader.Close();
            }

            return danhMucThongTinList;

        }
        private static DanhMucThongTin LoadItemFromReader(IDataReader reader)
        {
            DanhMucThongTin item = new DanhMucThongTin();
            if (reader.Read())
            {
                item.DanhMucGuid = new Guid(reader[ConstantVariable.LIST_DANHMUCTHONGTIN_GUID].ToString());
                item.DanhMucID = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_ID].ToString();
                item.DanhMucName = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_NAME].ToString();
                item.LoaiDanhMucGuid = new Guid(reader[ConstantVariable.LIST_DANHMUCTHONGTIN_KINDGUID].ToString());
                item.LoaiDanhMucName = reader[ConstantVariable.LIST_DANHMUCTHONGTIN_KINDNAME].ToString();
                item.IsDeleted = Convert.ToBoolean(reader[ConstantVariable.COLUMN_ISDELETED]);
                item.IsActive = Convert.ToBoolean(reader[ConstantVariable.COLUMN_ISACTIVE]);

            }
            return item;

        }
        #region public methods
        public List<DanhMucThongTin> GetAllLoaiDanhMuc()
        {
            IDataReader reader = DanhMucThongTinDAL.GetAllKind();
            return LoadListFromReader(reader);
        }
        public bool Save(DanhMucThongTin loaiDanhMuc)
        {
            if (loaiDanhMuc.DanhMucGuid != Guid.Empty)
            {
                return Update(loaiDanhMuc);
            }
            else
            {
                return Create(loaiDanhMuc);
            }
        }
        public bool CheckExitLoaDanhMuc(string danhMucName,Guid loaiDanhMucGuid)
        {
            DanhMucThongTin item = new DanhMucThongTin();
            IDataReader reader = DanhMucThongTinDAL.CheckExitLoaDanhMuc(danhMucName, loaiDanhMucGuid);
            item = LoadItemFromReader(reader);
            if (item.DanhMucGuid != Guid.Empty)
                return true;
            else
                return false;
        }
        public List<DanhMucThongTin> GetAllKindByIsActive(bool isActive, Guid loaiDanhMucGuid)
        {
            IDataReader reader = DanhMucThongTinDAL.GetAllKindByIsActive(isActive, loaiDanhMucGuid);
            return LoadListFromReader(reader);
        }
        public List<DanhMucThongTin> GetOne(Guid danhMucGuid)
        {
            IDataReader reader = DanhMucThongTinDAL.GetOne(danhMucGuid);
            return LoadListFromReader(reader);
        }
        public bool DeleteKind(Guid DataKeyValue)
        {
            return DanhMucThongTinDAL.DeleteKind(DataKeyValue);
        }
        public bool CheckExitDanhMuc(string danhMucName, Guid loaiDanhMucGuid)
        {
            DanhMucThongTin item = new DanhMucThongTin();
            IDataReader reader = DanhMucThongTinDAL.CheckExitDanhMuc(danhMucName, loaiDanhMucGuid);
            item = LoadItemFromReader(reader);
            if (item.DanhMucGuid != Guid.Empty)
                return true;
            else
                return false;
        }
        #endregion
    }
}
